class Solution
{
public:
    long long maxAlternatingSum(vector<int> &nums)
    {
        int n = nums.size();
        long long dp0 = 0;
        long long dp1 = nums[0];

        long long result = 0;
        for (int i = 0; i < n; ++i)
        {
            long long dp0New = max(dp0, dp1 - nums[i]);
            long long dp1New = max(dp1, dp0 + nums[i]);
            dp0 = dp0New;
            dp1 = dp1New;

            result = max({result, dp0, dp1});
        }

        return result;
    }
};